/***********************************************************************
 * Module:  PipeManager.java
 * Author:  Jean-Fran�ois FERNANDES, Alex Rameaux WAFO DEFO
 * Purpose: Defines the Class PipeManager (main)
 ***********************************************************************/
package event;
public class PipeManager {

	private Pipe<Temperature> temperaturePipe;
	private Pipe<Humidity> humidityPipe;
	private Pipe<Luminosity> luminosityPipe;
	
	private Pipe<ComplexEvent> compleEventPipe;
	
	public PipeManager(){
	
		  temperaturePipe = new Pipe<Temperature>();
		  humidityPipe = new Pipe<Humidity>();
		  luminosityPipe = new Pipe<Luminosity>();
		  
		  compleEventPipe = new Pipe<ComplexEvent>();
		  
		  new Writer().start();	  
		  
	 }
	
	
	public void test(){
		int temp = (int) (Math.round(Math.random()));
		int lum = (int) (Math.round(Math.random()));
		int hum = (int) (Math.round(Math.random()));
		int com = (int) (Math.round(Math.random()));
		
		int signe;
		int donnee;
		String capteur;
		int duree;
		
		if (temp > 0) {
			signe = (-1)*(int)(Math.random());
			donnee = signe * (int)(Math.random() * 40);
			capteur = "Source " + (int)(Math.random() * 5);
			duree = (int)(1 + Math.random() * 9);
			Temperature temperature1 = new Temperature("" + donnee, capteur, duree);
			temperaturePipe.push(temperature1);			
		}
		 
		if (lum > 0) {
			donnee = (int)(Math.random() * 100);
			capteur = "Source " + (int)(Math.random() * 5);
			duree = (int)(1 + Math.random() * 9);
			  
			Luminosity lumi1 = new Luminosity(donnee + "%", capteur, duree);
			luminosityPipe.push(lumi1);		  
			
		}
		
		if (hum > 0) {
			donnee = (int)(Math.random() * 100);
			capteur = "Source " + (int)(Math.random() * 5);
			duree = (int)(1 + Math.random() * 9);
			  
			Humidity humi1 = new Humidity(donnee + "%", capteur, duree);
			humidityPipe.push(humi1);		  
			
		}		
		
		if (com > 0) {
			donnee = (int)(Math.random() * 100);
			capteur = "Source " + (int)(Math.random() * 5);
			duree = (int)(1 + Math.random() * 9);
			  
			ComplexEvent com1 = new ComplexEvent("D" + donnee, capteur, duree);
			compleEventPipe.push(com1);		  
			
		}
		
		afficherPipe(temperaturePipe);
		afficherPipe(luminosityPipe);
		afficherPipe(humidityPipe);
		afficherPipe(compleEventPipe);
		
	}

		public void afficherPipe(Pipe pipe){
			if (pipe.getEvents().size() > 0) {
				String evt = "File " + pipe.getEvents().get(0).getClass().toString().split(" ")[1];
				System.out.println(evt.toUpperCase() + " *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*");
			}
		  for(int i = 0; i < pipe.getEvents().size(); i++){
			  Event event = (Event) pipe.getEvents().get(i);
			  System.out.println(i + "- " + event.getData() + " --> Dur�e de vie = " + event.getDuration());
		  }
		  System.out.println("Sommet : " + pipe.getSommet());
		  System.out.println();
		 }
		 
		 /**
		 * @param args
		 */
		 public static void main(String[] args) {
		  try{
		   PipeManager manager = new PipeManager();
		   manager.test();
		  }catch (Exception e) {
		   e.printStackTrace();
		  }
		 }
		
		 /**
		  * Thread qui permet d'ajouter des �l�ments dans les diff�rentes files.
		  * Cette classe devra �tre rajout� dans la classe qui contient le main 
		  * de l'application finale.
		  * 
		  * @author Jean-Fran�ois FERNANDES, Alex Rameaux WAFO DEFO
		  * 
		  */
		 public class Writer extends Thread {
			 public synchronized void run() {
				 while(true) {
					 try {
						 Thread.sleep(2000);
						 test();
					 } catch (InterruptedException e) {
						e.printStackTrace();
					 }
				 }
			 }
		 }
}
